package test;
import static org.junit.Assert.*; 


import org.junit.*;


import dsa.LinkedList;
import dsa.ListIterator;

public class JohnCostantinoTest {
	
	
	LinkedList testLink;
	ListIterator it; 
	
	@Before
	public void setUp() throws Exception
	{	
		testLink = new LinkedList();
		it = testLink.listIterator(0);
		
		testLink.addLast("item 0");
		testLink.addLast("item 1");
		testLink.addLast("item 2");
		testLink.addLast("item 3");
		testLink.addLast("item 4");
		testLink.addLast("item 5");
	}
	
	@After
	public void tearDown() throws Exception
	{

	}

	@Test
	public void listTestOne()throws Exception
	{	
		assertTrue(testLink.size()==6);
		assertFalse(testLink.size()==4);
		assertTrue(testLink.getFirst().equals("item 0"));
		assertTrue(testLink.getLast().equals("item 5"));
		assertTrue(testLink.get(3).equals("item 3"));
		assertFalse(testLink.get(5).equals("item 6"));
		assertTrue(testLink.contains("item 0"));
		assertTrue(testLink.contains("item 1"));
		assertTrue(testLink.contains("item 2"));
		assertTrue(testLink.contains("item 3"));
		assertTrue(testLink.contains("item 4"));
		assertTrue(testLink.contains("item 5"));
		assertTrue(testLink.indexOf("item 4")==4);
		assertFalse(testLink.indexOf("item 4")==3);
		assertFalse(testLink.indexOf("item 4")==8);
		assertTrue(testLink.indexOf("item 9")== -1);
	}


	@Test
	public void listTestTwo()throws Exception
	{
		testLink.remove(3);
		assertTrue(testLink.size()==5);
		assertFalse(testLink.contains("item 3"));
		testLink.addFirst("second");
		testLink.addFirst("first");
		assertTrue(testLink.size()==7);
		assertTrue(testLink.contains("second"));
		assertTrue(testLink.contains("item 2"));
		assertTrue(testLink.contains("item 0"));
		testLink.set(3, "newest 3");
		assertTrue(testLink.get(3).equals("newest 3"));
		assertFalse(testLink.get(2).equals("item 4"));
		assertTrue(testLink.getFirst().equals("first"));
		assertTrue(testLink.getLast().equals("item 5"));
		testLink.addLast("final");
		assertTrue(testLink.getLast().equals("final"));
	}

	@Test
	public void listTestClone()throws Exception
	{
		LinkedList clonedTest = new LinkedList();
		clonedTest = (LinkedList)testLink.clone();
		assertTrue(testLink.size()==clonedTest.size());
		assertTrue(testLink.getFirst().equals(clonedTest.getFirst()));
		assertTrue(testLink.getLast().equals(clonedTest.getLast()));
		assertTrue(testLink.get(3).equals(clonedTest.get(3)));
		clonedTest.remove(4);
		assertTrue(clonedTest.size()==5);
		assertTrue(clonedTest.get(4).equals("item 5"));
	}

	@Test
	public void listTestToArray()throws Exception
	{	
		Object [] testArray = testLink.toArray();
		assertTrue(testArray.length==testLink.size());
		assertTrue(testArray[0].equals(testLink.get(0)));
		assertTrue(testArray[1].equals(testLink.get(1)));
		assertTrue(testArray[2].equals(testLink.get(2)));
		assertTrue(testArray[3].equals(testLink.get(3)));
		assertTrue(testArray[4].equals(testLink.get(4)));
		assertTrue(testArray[5].equals(testLink.get(5)));
		assertFalse(testArray[4].equals(testLink.get(3)));			
	}

	@Test
	public void listTestFinal()throws Exception
	{
		testLink.removeLast();
		testLink.remove(3);
		testLink.set(2, "new 1");
		testLink.removeFirst();
		testLink.addLast("new Last");
		Object[] testArray = testLink.toArray();
		assertTrue(testArray.length==testLink.size());
		assertTrue(testArray[0].equals(testLink.get(0)));
		assertTrue(testArray[1].equals(testLink.get(1)));
		assertTrue(testArray[2].equals(testLink.get(2)));
		assertTrue(testArray[3].equals(testLink.get(3)));
		assertFalse(testArray[2].equals(testLink.get(3)));	
	}
	
	//item 0 / item 1 / item 2 / item 3 / item 4 / item 5
	@Test
	public void TestLIadd() throws Exception
	{
		
	}
	@Test
	public void TestLIhasNextOrPrevious() throws Exception
	{
		assertTrue(it.hasNext() == true);
		assertTrue(it.next().equals("item 0"));
		assertTrue(it.hasNext() == true);
		assertTrue(it.hasPrevious() == true);
		assertTrue(it.next().equals("item 1"));
		assertTrue(it.hasNext() == true);
		assertTrue(it.hasPrevious() == true);
	}
	@Test
	public void TestLINextOrPreviousIndex() throws Exception
	{	
		assertTrue(it.nextIndex() == 0);
		it.next();
		assertTrue(it.previousIndex() == 0);
		assertTrue(it.nextIndex() == 1);
		it.next();
		assertTrue(it.previousIndex() == 1);
		assertTrue(it.nextIndex() == 2);
		it.next();
		assertTrue(it.previousIndex() == 2);
		assertTrue(it.nextIndex() == 3);
		it.next();
		assertTrue(it.previousIndex() == 3);
		assertTrue(it.nextIndex() == 4);
		it.next();
		assertTrue(it.previousIndex() == 4);
		assertTrue(it.nextIndex() == 5);
		it.previous();
		assertTrue(it.previousIndex() == 3);
		assertTrue(it.nextIndex() == 4);
	}
	@Test
	public void TestLIremove() throws Exception
	{
		it.next(); //0
		it.next(); //1
		it.remove(); //1
		assertTrue(it.previous().equals("item 2"));
		assertTrue(it.next().equals("item 2"));
		assertTrue(it.nextIndex() == 1);
		
	}
	@Test
	public void TestLIset() throws Exception
	{	
		it.next();
		assertTrue(it.next().equals("item 1"));
		it.set("new");
		assertTrue(it.previous().equals("new"));
		assertTrue(it.next().equals("new"));
		assertTrue(it.next().equals("item 2"));
		assertTrue(it.previous().equals("item 2"));
	}
	
}
